package wechat.service.servlet;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import wechat.handler.bean.request.menu.MenuBean;
import wechat.handler.request.access.IAccess;
import wechat.handler.request.access.impl.Access;
import wechat.handler.request.menu.IMenu;
import wechat.handler.request.menu.impl.Menu;
import wechat.handler.util.crypto.CryptoUtil;

/**
 * Servlet implementation class EmergencyServlet
 */
@WebServlet("/EmergencyServlet")
public class EmergencyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger log = LoggerFactory.getLogger(EmergencyServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmergencyServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * 紧急重置各种token
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String type = request.getParameter("type");
		String verifyKey = request.getParameter("verifyKey");
		
		String appid = request.getParameter("appid");
		String selectType = request.getParameter("selectType");
		String menuinfo = request.getParameter("menuinfo");
		
		log.info("EmergencyServlet type=" + type);
		log.info("EmergencyServlet menuinfo=" + menuinfo);
		
		
		SimpleDateFormat  simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH");
		try {
			boolean verifyFlag = verifyKey.equals(CryptoUtil.getHmacMD5("app", simpleDateFormat.format(new Date())));
			log.info("EmergencyServlet verifyFlag=" + verifyFlag);
			
			if (verifyFlag) {
				
				if (type.equals("access")) {
					access(appid, selectType);
				}
				
				if (type.equals("menu")) {
					menu(menuinfo);
				}
				
				request.setAttribute("messages", "success");
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index2.jsp");
				requestDispatcher.forward(request, response);
				
				return;

			}
		} catch (InvalidKeyException e) {
			log.error("EmergencyServlet fail",e);
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			log.error("EmergencyServlet fail",e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error("EmergencyServlet fail",e);
			e.printStackTrace();
		}
		
		request.setAttribute("messages", "fail");
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index2.jsp");
		requestDispatcher.forward(request, response);

	}

	private void menu(String menuinfo) throws Exception {
		IMenu menu = new Menu();

		MenuBean menuBean = JSON.parseObject(menuinfo, MenuBean.class);
		menu.createMenu(menuBean);
	}

	private void access(String appid, String selectType) throws Exception {
		IAccess access = new Access();

		if (selectType.equals("1")) {
			access.resetAccessToken();
		}
		if (selectType.equals("2")) {
			access.resetJSTicket();
		}
		if (selectType.equals("3") && !appid.equals("")) {
			access.resetAccessToken(appid);
		}

	}

}
